package com.glut.library.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glut.library.entity.Book;
import com.glut.library.mapper.BookMapper;
import com.glut.library.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * 书籍服务实现类
 * @author stubborn
 * @since 2024-12-13 16:28
 */

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper,Book> implements IBookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public List<Book> getAllBooks() {
        return bookMapper.selectList(null);  // 获取所有书籍
    }

    @Override
    public List<Book> searchBooks(String query) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();

        // 如果查询字符串不为空，则添加搜索条件
        if (query != null && !query.trim().isEmpty()) {
            queryWrapper.like("book_name", query)
                    .or().like("author", query)
                    .or().like("publishing_house", query);
        }

        // 返回符合条件的书籍列表
        return bookMapper.selectList(queryWrapper);
    }
}